package cn.com.listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.log4j.Logger;

public class SessionAttributeListenerTest implements HttpSessionAttributeListener {

	private Logger logger = Logger.getLogger(SessionAttributeListenerTest.class);

	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		// 添加属性时被调用
		HttpSession session = arg0.getSession();// 发生变化的session
		String name = arg0.getName();// 新添加的属性名
		logger.info("新建session属性：" + name + ",值为：" + arg0.getValue());

	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		// 删除属性前被调用
		HttpSession session = arg0.getSession();
		String name = arg0.getName();
		logger.info("删除session属性：" + name + ",值为：" + arg0.getValue());

	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// 修改属性时被调用
		HttpSession session = arg0.getSession();
		String name = arg0.getName();
		Object oldValue = arg0.getValue();// 原来属性值
		logger.info("修改session的属性：" + name + ",原值：" + oldValue + ",新值：" + session.getAttribute(name));

	}

}
